package com.shenhua.kunpeng.db;

import com.alibaba.fastjson.JSONObject;
import com.shenhua.kunpeng.config.HikariCofig;
import com.shenhua.kunpeng.config.HikariOtherCofig;
import com.shenhua.kunpeng.config.HikariSlaveCofig;
import com.shenhua.kunpeng.utils.DataSourceUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Import({HikariCofig.class, HikariOtherCofig.class, HikariSlaveCofig.class})
public class DbConfig {
    @Bean(name = {"dynamicDataSource"})
    @Primary
    public DataSource dynamicDataSource(HikariCofig hikariCofig, HikariOtherCofig otherCofig,HikariSlaveCofig slaveCofig) {
        DataSource defaultDataSource = DataSourceUtil.createDataSource(hikariCofig);
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put("main", defaultDataSource);

        if (otherCofig != null && otherCofig.getConfs() != null && !otherCofig.getConfs().isEmpty()) {
            Map<String, Map> confs = otherCofig.getConfs();
            for (Map.Entry<String, Map> entry : confs.entrySet()) {
                try {
                    HikariCofig otherHikariCofig = JSONObject.parseObject(JSONObject.toJSONString(entry.getValue()), HikariCofig.class);
                    dataSourceMap.put(entry.getKey(), DataSourceUtil.createDataSource(otherHikariCofig));
                } catch (Exception e) {
                    throw new RuntimeException("初始化其它数据库异常，需要检查相关配置:" + entry.getKey(), e);
                }
            }
        }
        if (slaveCofig != null) {
            HikariCofig slave = new HikariCofig();
            BeanUtils.copyProperties(slaveCofig,slave);
            DataSource slaveDataSource = DataSourceUtil.createDataSource(slave);
            dataSourceMap.put("read", slaveDataSource);
        }

        //设置动态数据源
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        dynamicDataSource.setDefaultTargetDataSource(defaultDataSource);
        return dynamicDataSource;
    }

    @Bean({"dataSourceTransactionManager"})
    @ConditionalOnBean({DataSource.class})
    public PlatformTransactionManager dataSourceTransactionManager(@Qualifier("dynamicDataSource") DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
